package com.gitee.wsl.collections.iterator

fun <T> T.asIterator() = SingletonIterator(this)

/**
 * Iterator producing a single [element].
 *
 * @param element The element to be produced by the iterator.
 *
 * @param T Type of the [element].
 */
class SingletonIterator<T>(private val element: T) : Iterator<T> {

    private var hasNext = true

    override fun hasNext(): Boolean = hasNext
    override fun next(): T =
        if (hasNext) {
            hasNext = false
            element
        } else {
            throw NoSuchElementException("Only one element available")
        }
}